<template>
	<view>
		<!-- <view class="status_bar">
			
		</view> -->
		<!-- <uni-nav-bar backgroundColor="transparent" left-icon="left" right-icon="notification" color="#283241"
			:border="false" title="网约农服详情" @clickLeft="goBack()" @clickRight="goTo('../notice/notice')">
		</uni-nav-bar> -->
		<image class="details-photo" :src='imgUrl' mode="widthFix">
		</image>
		<view v-if="arctileObj" style="padding-bottom: 50px;">
			<view class="content" style="padding-bottom: 50px;">
				<view class="uni-flex" style="justify-content: space-between; align-items: center;">
					<!-- <text class="price">¥{{arctileObj.price}}</text> -->
					<uni-fav :checked="isFav" :circle="true" bg-color="#B4B5B7" @click="setFav(isFav)"
						bg-color-checked="rgba(255, 90, 90, 0.08)" fg-color="#ffffff"
						fg-color-checked="#FF5A5A"></uni-fav>
				</view>
				<view class="title"><text class="label">{{sort}}</text>{{arctileObj.title}}</view>
				<view class="contact">
					<view class="company">{{arctileObj.providerName}}</view>
					<view class="uni-flex fs12" style="justify-content: space-between; align-items: center;">
						<view><uni-icons type="phone-filled" size="14"
								color="#00B5DD"></uni-icons><text @click="TakePhone(arctileObj.phone)">{{arctileObj.phone}}</text></view>
						<view v-if="arctileObj.weChat"><uni-icons type="weixin" size="14"
								color="#00B5DD"></uni-icons><text @longpress="getWechat(arctileObj.weChat)">{{arctileObj.weChat}}</text></view>
					</view>
				</view>
				<view class="summary" v-if="arctileObj.resume">
					<view class="title"><uni-icons type="calendar-filled" color="#00B5DD" size="16"></uni-icons><text
							style="margin-left: 15rpx;">摘要</text></view>
					<text class="con">{{arctileObj.resume}}</text>
				</view>
				<view class="art" v-if="arctileObj.description"><rich-text :nodes="nodes"></rich-text></text>
				</view>
				<button class="customer-btn" @click="TakePhone(arctileObj.phone)">立即联系<uni-icons type="phone" color="#ffffff"
						size="16"></uni-icons></button>
			</view>
		</view>
		<view class="uni-loadmore" v-if="showLoadMore">加载中...</view>
		
	</view>
</template>

<script>
	import {
		detailFarmWork,
		detailFarmSeek,
		queryDictDataTree,
		baseurl,
		deleteCollection,
		addCollection
	} from "@/api/user.js";
	import permision from "@/js_sdk/wa-permission/permission.js";
	export default {
		data() {
			return {
				parmas: '',
				arctileObj: null,
				isFav: false,
				typeList: [],
				sort: '',
				imgUrl: '',
				showLoadMore: false,
				serviceType:'',
				nodes: [],
			};
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			console.log("options",option); //打印出上个页面传递的参数。
			this.parmas = option.id;
			this.showLoadMore = true;
			this.serviceType = option.sort
			console.log("this.serviceType",this.serviceType)
			if(this.serviceType == '5'){
				this.detailFarmWork(this.parmas);
			}else{
				uni.setNavigationBarTitle({
					title: '农户求助详情'
				});
				this.detailFarmSeek(this.parmas);
			}
		},
		onPullDownRefresh(){
			if(this.serviceType == '5'){
				this.detailFarmWork(this.parmas);
			}else{
				uni.setNavigationBarTitle({
					title: '农户求助详情'
				});
				this.detailFarmSeek(this.parmas);
			}
			uni.stopPullDownRefresh();
		},
		methods: {
			goTo(e) {
				var url = e;
				uni.navigateTo({
					url: url
				})
			},
			goBack() {
				uni.navigateBack()
			},
			async queryDictDataTree(options) {
				const res = await queryDictDataTree(options)
				console.log(res);
				let temp = JSON.stringify(res.returnObj);
				//temp = temp.replaceAll("subDictDataList", "children")
				temp = temp.replace(/subDictDataList/g,"children")
				this.typeList = JSON.parse(temp)
				//console.log("parmas",this.arctileObj.catgory)
				if(this.arctileObj.catgory){
					//console.log("parmas")
					this.sort = this.getSortName(this.arctileObj.catgory, this.typeList)
				}
				
			},
			async detailFarmWork(options) {
				const res = await detailFarmWork(options);
				console.log(res)
				this.arctileObj = res.returnObj;
				this.nodes = this.arctileObj.description;
				this.showLoadMore = false;
				this.queryDictDataTree({
					"dictType": "SERVICE_CATGORY"
				})
				if(this.arctileObj.myFavorite > 0){
					this.isFav = true
				}else{
					this.isFav = false
				}
				if (this.arctileObj.image) {
					uni.downloadFile({
						url: `${baseurl}/nb-as/1.0.1/FileService/downloadFile?fileId=${this.arctileObj.image}`, 
						success: (res) => {
							if (res.statusCode === 200) {
								console.log("image", res);
								this.imgUrl = res.tempFilePath
							}
						}
					});
				}else{
					this.imgUrl = "https://staticfile-yj.obs.cn-north-4.myhuaweicloud.com/none.png"
				}
			},
			async detailFarmSeek(options) {
				const res = await detailFarmSeek(options);
				console.log(res)
				this.arctileObj = res.returnObj;
				if(this.arctileObj.description){
					this.nodes = this.arctileObj.description;
				}
				if(this.arctileObj.myFavorite > 0){
					this.isFav = true
				}else{
					this.isFav = false
				}
				this.queryDictDataTree({
					"dictType": "SERVICE_CATGORY"
				})
				this.showLoadMore = false;
				if (this.arctileObj.image) {
					uni.downloadFile({
						url: `${baseurl}/nb-as/1.0.1/FileService/downloadFile?fileId=${this.arctileObj.image}`, //仅为示例，并非真实的资源
						success: (res) => {
							if (res.statusCode === 200) {
								console.log("image", res);
								this.imgUrl = res.tempFilePath
							}
						}
					});
				}
			},
			
			async addCollection(options){
				const res = await addCollection(options);
				if(res.success){
					uni.showToast({
						title:'收藏成功！'
					})
					this.isFav = true
				}
			},
			
			async deleteCollection(options){
				const res = await deleteCollection(options);
				if(res.success){
					uni.showToast({
						title:'已取消收藏！'
					})
					this.isFav = false
				}
			},
			
			getWechat(item){
				uni.setClipboardData({
					data: item,
					
				});
			},
			TakePhone(e) {
				permision.requestAndroidPermission('android.permission.CALL_PHONE').then((res)=>{
					console.log("authres",res)
					if(res == 1){
						uni.makePhoneCall({
							phoneNumber: e
						});
					}else{
						permision.gotoAppPermissionSetting()
					}
				})
			},
			setFav() {
				let options = {
					serviceType:this.serviceType,
					serviceId:this.arctileObj.id
				}
				if(this.isFav){
					this.deleteCollection([options])
				}else{
					this.addCollection(options)
				}
				this.$forceUpdate()
			},
			getSortName(parmas, list) {
				console.log("parmas", parmas)
				if (!parmas) {
					return
				}
				let name = '';
				list.forEach(el => {
					if (el.children) {
						el.children.forEach(h => {
							if (h.code == parmas) {
								name = h.name;
							}
						})
					} else {
						if (el.code == parmas) {
							name = el.name;
						}
					}
				})
				console.log("name", name)
				return name
			}
		}
	}
</script>

<style lang="scss">
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.details-photo {
		width: 100%;
	}

	.content {
		background-color: #ffffff;
		border-radius: 16px 16px 0 0;
		padding: 30rpx;
	}

	.content .title {
		font-size: 36rpx;
		color: #283241;
		line-height: 26px;
		font-weight: 500;
	}

	.content .title .label {
		padding: 4rpx 8px;
		background-color: #EBF9FC;
		border-radius: 4px;
		color: #00B5DD;
		font-size: 24rpx;
		margin-right: 5px;
	}

	.price {
		font-size: 40rpx;
		color: #FF5A5A;
	}

	.contact {
		background-color: #F5F8FB;
		padding: 30rpx;
		border-radius: 8px;
		margin-top: 15px;
	}

	.contact .company {
		font-size: 32rpx;
		color: #283241;
		line-height: 24px;
		height: 24px;
		font-weight: 500;
		margin-bottom: 10px;
	}

	.summary {
		background-color: #F5F8FB;
		border-radius: 8px;
		padding: 24rpx 32rpx;
		margin: 15px 0;
	}

	.summary .title {
		font-size: 36rpx;
		line-height: 26px;
		color: #283241;
		flex-flow: row nowrap;
		justify-content: flex-start;
		align-items: center;
	}

	.summary .con {
		line-height: 26px;
		font-size: 28rpx;
		font-weight: 400;
		color: #283241;
	}

	.art text {
		font-size: 28rpx;
		line-height: 26px;
	}

	.customer-btn {
		background-color: #00B5DD !important;
		color: #fff;
		position: fixed;
		bottom: 30rpx;
		left: 30rpx;
		right: 30rpx;
		border-radius: 8px;
		height: 44px;
		line-height: 44px;
		font-size: 32rpx;
		font-weight: 500;
	}
</style>